package List.LinkList;

/**
 * 排序插入-单链表
 */
public class AscLinkList {

    private Node head;

    public void show() {
        Node temp;
        temp = head;
        while (true) {
            if (temp.next == null) {
                System.out.print(temp.intData+"\t");
                break;
            } else {
                System.out.print(temp.intData+"\t");
                temp = temp.next;
            }
        }
        System.out.println();

    }

    //升序插入
    public void addAsc(int intData){

        //链表为空
        if(head == null){
            head = new Node(intData);
        //数据小于头节点
        }else if(intData < head.intData){
            Node temp = new Node(intData);
            temp.next=head;
            head=temp;
        }else{
            Node temp = head;
            while (true) {
                //尾节点
                if(temp.next==null){
                    temp.next = new Node(intData);
                    break;
                //中间插入
                }else if(intData < temp.next.intData ){
                    Node newNode = new Node(intData);
                    newNode.next=temp.next;
                    temp.next=newNode;
                    break;

                //下一个
                }else{
                    temp=temp.next;
                }
            }
        }

    }
}
